﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TaskCrossVisit
{
    public partial class FrmTask : Form
    {
        public FrmTask()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Task task = new Task(() =>
            {              
               this.lblInfo.Text = "来自Task的任务信息：学了C#高级编写内容";
            });

            //这个就是解决跨线程访问控件的方法
            task.Start(TaskScheduler.FromCurrentSynchronizationContext());
        }

        //耗时卡顿现象
        private void appearance_Click(object sender, EventArgs e)
        {
            Task task = new Task(() =>
            {
                //在这里编写具体的任务...

                Thread.Sleep(5000); //模拟耗时任务
                this.lblInfo.Text = "来自Task的任务信息：学了C#高级编写内容";
            });

            //这个就是解决跨线程访问控件的方法
            task.Start(TaskScheduler.FromCurrentSynchronizationContext());
        }
        
        //解决耗时卡顿
        private void btnSolve_Click(object sender, EventArgs e)
        {
            Task task = Task.Run(() =>
            {
                //在这里编写具体的任务...

                Thread.Sleep(5000); //模拟耗时任务                
            });

            task.ContinueWith(t =>
            {
                this.lblInfo.Text = "来自Task的任务信息：学了C#高级编写内容";
            }, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
}
